package com.sxf.rental.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxf.rental.entity.Permission;
import com.sxf.rental.entity.User;
import com.sxf.rental.mapper.PermissionMapper;
import com.sxf.rental.mapper.UserMapper;
import com.sxf.rental.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxf.rental.utils.RouteTreeUtils;
import com.sxf.rental.vo.RolePermissionVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sxf
 * @since 2025-03-08
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Resource
    private UserMapper userMapper;
    @Override
    public List<Permission> selectPermissionListByUserId(Integer userId) {
        return baseMapper.selectPermissionListByUserId(userId);
    }

    @Override
    public boolean hasChildren(Integer id) {
        QueryWrapper<Permission> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pid",id);
        return baseMapper.selectCount(queryWrapper)>0;
    }

    @Override
    public RolePermissionVo selectRolePermissionTree(Integer userId, Integer roleID) {
        User user=userMapper.selectById(userId);
        List<Permission> list=null;
        //如果用户是管理员,查询所有的权限信息
        if (user!=null&&user.getIsAdmin()==1){
            list=baseMapper.selectList(null);
        }else {
            //否则不是管理员，查询当前所用的权限信息
            list=baseMapper.selectPermissionListByUserId(userId);
        }
        //将登陆用户所拥有的权限信息封装的一个树形结构中
        List<Permission> permissionList = RouteTreeUtils.buildMenuTree(list, 0);
        //查询出要分配的角色他原有的权限
        List<Permission> rolePermissionList = baseMapper.selectPermissionListByRoleId(roleID);
        //找出包含的部分，放在Object[]数组中
        //ids.retainAll(ids1); 是一个取交集的修改操作，它会改变原列表的内容。
        //toList()在Java 8中新添加的，用于将Stream流转换为List或数组，但是这个List是一个不可变对象 因为new ArrayList<> 来复制这个不可变List
        List<Integer> ids = new ArrayList<>(list.stream().map(Permission::getId).toList());
        List<Integer> ids1 = rolePermissionList.stream().map(Permission::getId).toList();
        ids.retainAll(ids1);
        Object[] array = ids.toArray();
        RolePermissionVo rolePermissionVo=new RolePermissionVo();
        rolePermissionVo.setCheckedList(array).setPermissionList(permissionList);
        return rolePermissionVo;
    }

    @Override
    public List<Permission> tree() {
        QueryWrapper<Permission> queryWrapper=new QueryWrapper<>();
        queryWrapper.ne("permission_type",2);
        queryWrapper.orderByAsc("order_num");
        List<Permission> permissionList = baseMapper.selectList(queryWrapper);
        Permission permission=new Permission();
        permission.setId(0).setPid(-1).setPermissionLabel("根目录");
        permissionList.add(permission);
        return RouteTreeUtils.buildMenuTree(permissionList,-1);
    }

    @Override
    public List<Permission> selectList() {
        QueryWrapper<Permission> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByAsc("order_num");
        List<Permission> permissionList = baseMapper.selectList(queryWrapper);
        return RouteTreeUtils.buildMenuTree(permissionList,0);
    }
}
